﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Collections;
using Microsoft.Office.Core;


namespace appLibrary
{
   public  class SendMail
    {



       public static void sendMail2(string strServerExchange, string strFrom, string[] arrayTo, string strSubject,
           string strMessage, string[] arrayAttachment)
       {
       

           System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add("miguel.rivera@nyrstar.com");
            msg.From = new MailAddress("miguel.river@nyrstar.com", "Miguel", System.Text.Encoding.UTF8);
            msg.Subject = "Prueba de correo a GMail";
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = "Cuerpo del mensaje";
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = false; 
          

            //Aquí es donde se hace lo especial
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("mrivera.scorpio@gmail.com", "m261082**rr");
           // client.Credentials = new System.Net.NetworkCredential("riveram26", "Welcome1");
            client.Port = 587;
            client.Host = "smtp.gmail.com";
            //client.Host = "SACEX02.saturnus.ads";
            client.EnableSsl = true; //Esto es para que vaya a través de SSL que es obligatorio con GMail
            try
            {
                        client.Send(msg);
                        Log.eLog("uknown", "Se envio el mensaje satisfactoriamente");
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Log.eLog("uknown", "(ERROR):"+ ex.ToString());
            }
       }


       public static bool sendMail(string strServerExchange,string strFrom, string[] arrayTo, string strSubject,
           string strMessage, string[] arrayAttachment, string strCodeInspection, string strNumObervation, string strNameResponsible ,string[] arrayObsPast, string strUser)
       {

           SmtpClient clienteSmtp = new SmtpClient(strServerExchange);
          // clienteSmtp.EnableSsl = true;
           try
           {

               MailMessage email = new MailMessage();
               MailAddress mailAddress = new MailAddress(strFrom);
               //MailAddress mailAddressTo=new MailAddress("miguel.rivera@nyrstar.com,mrivera.scorpio@gmail.com,marco.baldeon@nyrstar.com");
               //MailAddressCollection collTo=new MailAddressCollection();
               // collTo.Add("miguel.rivera@nyrstar.com");


               MailAddressCollection macoll = new MailAddressCollection();
               MailMessage msTo = new MailMessage();

               // macoll.Add(mailAddressTo);
               email.From = mailAddress;
               email.Bcc.Add("miguel.rivera@nyrstar.com");
               if (arrayTo.Length == 0)
                   throw new Exception("(ERROR): No existe ningún destinatario, favor de verificar");
               //Load to
               for (int i = 0; i < arrayTo.Length; i++)
               {
                   email.To.Add(arrayTo[i]);
               }


               email.Subject = strSubject;
               if (strMessage != null)
                   email.Body = strMessage;
               else

                   email.Body = "Estimado(a): " + strNameResponsible + "<br>Ud. Tiene una nueva inspecci&oacute;n con " + strNumObervation +
                       " Observaci&oacute;n(es) a levantar.<BR>El c&oacute;digo de la inspecci&oacute;n es " + strCodeInspection + "<BR>" +
                       "<BR>Observaciones Acumuladas:<BR>" +
                       " -Observaciones x Levantar: " + arrayObsPast[0] + "<BR> -Observaciones Vencidas:" + arrayObsPast[1]+
                       "<BR><BR>Atentamente:" +
                       "<BR><BR><FONT  face=Arial size=1 color=blue>Nyrstar Suport SIM<BR>"+
                       "support.sim@nyrstar.com |(+51) (1) 610-9501 ext. 1030 |(+51) (1) 241-3111 |www.nyrstar.com</FONT>"+
                        "<br><br> Favor de no responder a este  buz&oacute;n, ya que es un buz&oacute;n desatendido.";

              
          
                                 
               email.BodyEncoding = System.Text.Encoding.UTF8;

               //Attachment array in msn
               if (arrayAttachment!=null && arrayAttachment.Length > 0)
               {
                   Attachment[] attachment = new Attachment[arrayAttachment.Length];
                   for (int i = 0; i < arrayAttachment.Length;i++ )
                   {
                       attachment[i] = new System.Net.Mail.Attachment(arrayAttachment[i]);
                       email.Attachments.Add(attachment[i]);
                   }
               }
                //lo añadimos al mail
                
               //email.BodyFormat = MailFormat.Text
               //email.Body.frm
               email.IsBodyHtml = true;
               
               email.Priority = MailPriority.High;


               // Este es el código nuevo

               clienteSmtp.Credentials =
               new NetworkCredential("riveram26", "m261082***rr");
               //clienteSmtp.EnableSsl = false;

               clienteSmtp.Send(email);

               Log.eLog(strUser, "Se envió el mensaje satisfactoriamente");
               return true;
           }
           catch (System.Net.Mail.SmtpException ex)
           {
               Log.eLog(strUser, "(ERROR):" + ex.ToString());
               return false;
           }
           finally
           {
                clienteSmtp.Dispose();
           }
            
           
           
       }


       public static bool SendMailOutLook(string strServerExchange, string strFrom, string[] arrayTo, string strSubject,
           string strMessage, string[] arrayAttachment, string strCodeInspection, string strNumObervation, string strNameResponsible, string[] arrayObsPast, string strUser)
       {
           Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();

 //Create the new message by using the simplest approach.
  Microsoft.Office.Interop.Outlook.MailItem oMsg =( Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem( Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

 //Add a recipient.
 // TODO: Change the following recipient where appropriate.
  Microsoft.Office.Interop.Outlook.Recipient oRecip =( Microsoft.Office.Interop.Outlook.Recipient)oMsg.Recipients.Add("miguel.rivera@nyrstar.com");
  oRecip.Resolve();
  try
  {
      //Asigna el asunto.
      oMsg.Subject = "Asunto";
      oMsg.Body = "body";
      String sSource = "E:/Manuales/Manual_PHP5_Basico.pdf";
      String sDisplayName = "Mi asunto";
      int iPosition = (int)oMsg.Body.Length + 1;
      int iAttachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;
      Microsoft.Office.Interop.Outlook.Attachment oAttach =
     oMsg.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName);
      // If you want to, display the message.
      //oMsg.Display(true);  //modal
      //Send the message.
      oMsg.Save();
      oMsg.Send();
      //Explicitly release objects.
      oRecip = null;
      //oAttach = null;
      oMsg = null;
      //oApp.Quit();
      oApp = null;
      return true;
  }
  catch (Exception ex)
  {
      Log.eLog(null, "(ERROR):" + ex.ToString());
      return false;
  }
  finally
  {
      
  }

       }



    }
}
